/**
 * 
 */
package id.web.faisalabdillah.xtimesheet.web.ctrl;

import java.util.ArrayList;
import java.util.List;

import id.web.faisalabdillah.xtimesheet.domain.TimeSheet;
import id.web.faisalabdillah.xtimesheet.domain.TimeSheetDetail;
import id.web.faisalabdillah.xtimesheet.services.ITimeSheetService;
import id.web.faisalabdillah.xtimesheet.services.IUserService;
import id.web.faisalabdillah.xtimesheet.web.dto.DTOJsonResult;
import id.web.faisalabdillah.xtimesheet.web.model.TimesheetModel;

import org.apache.commons.beanutils.BeanUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.datastore.KeyFactory;

/**
 * @author faitz
 *
 */
@Controller
@RequestMapping("/timesheet")
public class TimesheetCtrl {
	@Autowired
	private ITimeSheetService timesheetService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/createTimesheet")
	public String createTimesheet(Model model){
		TimesheetModel tm=new TimesheetModel();
		/* Initialization Sample */
		tm.setNama("Muhamad Faisal Abdillah");
		tm.setUid("77777777");
		tm.setProject_name("Permata");
		tm.setLocation("Bintaro");
		
		model.addAttribute("timesheetmodel", tm);
		return "timesheet/add-timesheet";
	}
	
	@RequestMapping(value="/updateDetail/{id}")
	public String updateDetail(Model model, @PathVariable long id){
		TimeSheet ts = timesheetService.findById(KeyFactory.createKey("TimeSheet", id));
		model.addAttribute("timesheet", ts);
		return "timesheet/detail-timesheet";
	}
	@RequestMapping(value="/generateTimesheet",method=RequestMethod.POST)
	public @ResponseBody DTOJsonResult<Object> createTimesheet(TimesheetModel timesheetmodel){
		
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		data.setSuccess(true);
		if(timesheetmodel ==null){
			data.setSuccess(false);
			data.setError("Unknown Error");
		}
		/* Sample get data from session*/
		TimeSheet ts=new TimeSheet();
		DateTime month=new DateTime(timesheetmodel.getMonth().getTime());
		try {
		ts.setMonth(timesheetmodel.getMonth());
		ts.setDisetujui(timesheetmodel.getDisetujui());
		ts.setLocation(timesheetmodel.getLocation());
		ts.setNama(timesheetmodel.getNama());
		ts.setPemeriksa(timesheetmodel.getPemeriksa());
		ts.setUid(timesheetmodel.getUid());
		ts.setProject_name(timesheetmodel.getProject_name());
		ts.setUser(userService.findById("faitz1989@gmail.com"));
		
		int bulan=month.monthOfYear().get();
		List<TimeSheetDetail> listTsd=new ArrayList<TimeSheetDetail>();
		while(bulan == month.monthOfYear().get()){
			System.out.println("tanggal:"+month.dayOfMonth().get());
			System.out.println("bulan:"+month.monthOfYear().get());
			TimeSheetDetail tsd=new TimeSheetDetail();
			tsd.setDate(month.toDate());
			tsd.setTimesheet(ts);
			listTsd.add(tsd);
			month=month.plusDays(1);
		}
		ts.setDetail(listTsd);
		if(timesheetService.insert(ts)){
				System.out.println(BeanUtils.describe(ts));
				System.out.println(ts.getId());
				data.setData(ts.getId().getId());
		}else{
			data.setSuccess(false);
			data.setError("Data gagal disimpan silahkan hubungi administrator");
		}
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
}
